<?php
include "includes/params.php";
include "lib/json_fix.php"; 
include $err_strings;
include $info_strings;

$q=$_POST['q'];
$qlike = $q.'%'; 

 
$con = oci_pconnect($username, $password, $database);   
if (!$con)
  {
    // Could't connect, show login form
    $m = oci_error();
    die($sm_connect_error. $m["message"]);
  }
  
session_start(); 
$con_user = $_SESSION['con_user'];
$con_pass = $_SESSION['con_pass'];
$user_consid = $_SESSION['user_consid'];
$user_issupperuser = $_SESSION['user_issupperuser'];

if (!isset($_SESSION['login'])) {
  die($im_user_not_set);
}
// param depend on superuser status
$execute_mainsql = true; 
  
 
  $countresult = oci_parse($con, "Select count(*) cnt from point p where p.point_accountnum like :aAccNum");
  oci_bind_by_name($countresult, ":aAccNum", $qlike);
  oci_execute($countresult);
  $CntRow = oci_fetch_array($countresult, OCI_ASSOC);

if ($user_issupperuser == 1) {
  echo $im_number_filtered_pnt.": ". $CntRow['CNT']. "<br>";
  if ($CntRow['CNT'] < 1000 & $CntRow['CNT'] >= 1) {    
    $execute_mainsql = true;
  } else {
    $execute_mainsql = false;
  }
}

if ($execute_mainsql){
    $result = oci_parse($con, "select * from point p where p.point_accountnum like :aAccNum");   
    oci_bind_by_name($result, ":aAccNum", $qlike); 
    oci_execute($result); 
    
  $total = $CntRow['CNT'];

   $page = $_POST['page']; 
   // get the requested page 
   $limit = $_POST['rows']; 
   // get how many rows we want to have into the grid 
   $sidx = $_POST['sidx']; 
   // get index row - i.e. user click to sort 
   $sord = $_POST['sord']; 
   // get the direction 
   if(!$sidx) $sidx =1; 
   $count = $total; 
   if( $count >0 ) {
     $total_pages = $count ;
   } else { 
     $total_pages = 0; 
   } 
   if ($page > $total_pages) $page=$total_pages; 
   $start = $limit*$page - $limit; 
   // do not put $limit*($page - 1) 
   $responce->page = $page; 
   $responce->total = $total_pages; 
   $responce->records = $count; 
   $i=0; 
   while($row = oci_fetch_array($result, OCI_ASSOC)) { 
     $responce->rows[$i]['id']=$row['POINT_ID']; 
     $responce->rows[$i]['cell']=array($row['POINT_ID'],$row['POINT_ID'],$row['POINT_ID'],$row['POINT_ID'],$row['POINT_ID'],$row['POINT_ID']); 
     $i++; 
   } 
   echo json_encode2($responce); 
  
    
//    echo "<table border='1'>
//      <tr>
//      <th>Firstname</th>
//      <th>Lastname</th>
//      <th>Age</th>
//      <th>Hometown</th>
//      <th>Job</th>
//      </tr>";
        
//    while($row = oci_fetch_array($result, OCI_ASSOC))
//      {
//        echo "<tr>";
//        echo "<td>" . $row['POINT_ID'] . "</td>";
//        echo "<td>" . $row['POINT_ID'] . "</td>";
//        echo "<td>" . $row['POINT_ID'] . "</td>";
//        echo "<td>" . $row['POINT_ID'] . "</td>";
//        echo "<td>" . $row['POINT_ID'] . "</td>";
//        echo "</tr>";
//      }
//    echo "</table>";
    // clear
    oci_free_statement($result);
  }     
                               
// Logoff from Oracle...
oci_free_statement($countresult);
oci_close($con);
?> 